<template>
  <div>
    <!-- 工单编号行 -->
    <div class="search">
      <el-form :inline="true" :model="params" class="demo-form-inline">
        <el-form-item label="合作商搜索:">
          <el-input v-model="params.name" size="small" clearable placeholder="请输入" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="small" icon="el-icon-search" class="btn" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 主体内容  新建 -->
    <div class="result">
      <div class="operation">
        <el-row>
          <el-button
            size="medium"
            icon="el-icon-circle-plus-outline"
            class="one"
            @click="addbuild"
          >新建</el-button>
        </el-row>
      </div>
      <div>
        <template>
          <el-table
            :data="PartnerData"
            style="width: 100%"
            :row-style="{ height: '44px' }"
            :cell-style="{ padding: '3px' }"
          >
            <el-table-column
              label="序号"
              type="index"
              :index="indexMethod"
              width="80"
            />
            <el-table-column prop="name" label="合作商名称" width="143" />
            <el-table-column prop="account" label="账号" width="142" />
            <el-table-column
              prop="vmCount"
              label="设备数量"
              width="142"
            />
            <el-table-column prop="ratio" label="分成比例" width="142" />
            <!-- :show-overflow-tooltip="true" -->
            <el-table-column
              prop="contact"
              label="联系人"
              width="142"
            />
            <el-table-column
              prop="mobile"
              label="联系电话"
              width="142"
            />
            <el-table-column label="操作" width="251">
              <template slot-scope="scope">
                <el-button
                  type="text"
                  class="detail"
                  @click="reset(scope.row)"
                >重置密码</el-button>
                <el-button
                  type="text"
                  class="detail"
                  @click="handleClick(scope.row)"
                >查看详情</el-button>
                <el-button
                  type="text"
                  class="detail"
                  @click="modifyFn(scope.row)"
                >修改</el-button>
                <el-button
                  type="text"
                  class="piramy"
                  @click="del(scope.row)"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </template>
      </div>
    </div>
    <!-- 新增 dialog -->
    <el-dialog
      :title="title"
      :visible="dialogVisible"
      width="630xp"
      @close="handleClose"
    >
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="140px" class="demo-ruleForm">
        <el-form-item label="合作商名称:" prop="name">
          <el-input
            v-model="ruleForm.name"
            maxlength="10"
            show-word-limit
            placeholder="请输入"
          />
        </el-form-item>
        <el-form-item label="联系人:" prop="contact">
          <el-input
            v-model="ruleForm.contact"
            maxlength="10"
            show-word-limit
            placeholder="请输入"
          />
        </el-form-item>
        <el-form-item label="联系电话:" prop="mobile">
          <el-input
            v-model="ruleForm.mobile"
            maxlength="11"
            show-word-limit
            placeholder="请输入"
          />
        </el-form-item>
        <el-form-item label="分成比例:" prop="ratio">
          <!-- <el-input
            v-model="ruleForm.ratio"
            show-word-limit
          /> -->
          <el-input-number
            v-model="ruleForm.ratio"
            show-word-limit
            placeholder="请输入"
            style="width:90%"
            controls-position="right"
            :min="0"
            :max="100"
            @change="handleChange"
          />
        </el-form-item>
        <!-- prop="addrrr" -->
        <el-form-item v-if="isShow" label="账号:" prop="account">
          <el-input
            v-model="ruleForm.account"
            placeholder="请输入"
            maxlength="18"
            show-word-limit
          />
        </el-form-item>
        <el-form-item v-if="isShow" label="密码:" prop="password">
          <el-input
            v-model="ruleForm.password"
            placeholder="请输入"
            maxlength="20"
            show-word-limit
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="addsubmite">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 查看详情 -->
    <el-dialog
      title="合作商详情"
      :visible="seeDetails"
      width="630px"
      @close="seeDetails = false"
    >
      <el-form :model="ruleForm" label-width="140px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="合作商名称:">{{ ruleForm.name }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系人:">{{ ruleForm.contact }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话:">{{ ruleForm.mobile }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="分成比例:">{{ ruleForm.ratio + '%' }}</el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
    <!-- 分页 -->
    <el-row type="flex" justify="center" align="middle" style="height: 60px">
      <el-col class="pstion">
        共{{ totalCount }}条记录
        第{{ params.pageIndex }}/{{ totalPage }}页
      </el-col>
      <el-pagination
        :page-size="params.pageSize"
        :total="totalCount"
        :current-page="params.pageIndex"
        layout="prev,next"
        prev-text="上一页"
        next-text="下一页"
        @current-change="currentChangeFn"
      />
    </el-row>
  </div>
</template>

<script>
import { Partners, addPartners, resetPartners, modifyPartners, delPartners } from '@/api'
export default {
  data() {
    return {
      // 查看详情
      seeDetails: false,
      // 新增  显示隐藏变量
      dialogVisible: false,
      ruleForm: {
        name: '', // 合作商名字
        account: '', // 账号
        password: '', // 密码
        ratio: 0, // 分成比例
        contact: '', // 联系人
        mobile: '' // 手机号
      },
      phone: '', // 联系电话
      // 校验
      rules: {
        name: [
          { required: true, message: '请输入' }
        ],
        account: [
          { required: true, message: '请输入' }
        ],
        password: [
          { required: true, message: '请输入' }
        ],
        ratio: [
          { required: true, message: '请输入' }
        ],
        contact: [
          { required: true, message: '请输入' }
        ],
        mobile: [
          { required: true, message: '请输入' }
        ]
      },

      // 合作商数据
      PartnerData: [],
      totalCount: null, // 总条数
      totalPage: '', // 页数
      params: {
        name: '', // 合作商名字
        pageIndex: 1, // 当前页码
        pageSize: 10 // 每页条数
      },
      // 点击修改 回显数据 不显示账号 密码
      isShow: true
    }
  },
  computed: {
    title() {
      return this.ruleForm.id ? '修改合作商' : '新增合作商'
    }
  },
  created() {
    this.getPartners()
  },
  methods: {
    // 删除提示
    del(val) {
      this.$confirm('确定要删除么', '友情提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(async() => {
        await delPartners(val.id)
        this.$message.success('删除成功')
        this.getPartners()
      })
    },
    // 修改 数据回显
    modifyFn(val) {
      console.log(val)
      this.isShow = false
      this.ruleForm = val
      this.dialogVisible = true
    },
    // 重置密码
    reset(row) {
      this.$confirm('确定要重置合作商密码吗？', '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error',
        // 居中布局
        center: true
      }).then(async() => {
        // 重置合作商密码
        await resetPartners(row.id)
        // 提示
        this.$message.success('重置成功')
        // 刷新页面
        this.getPartners()
      })
    },
    // 分成比列
    handleChange(value) {
      // 改变的分成 在赋值给所需数据
      // this.ruleForm.ratio = value
      console.log(value)
    },
    // 新建提交
    addsubmite() {
      // 兜底校验
      this.$refs.ruleForm.validate(async(valid) => {
        if (!valid) return
        if (this.ruleForm.id) {
          await modifyPartners(this.ruleForm)
        } else {
          // 调用接口
          await addPartners(this.ruleForm)
        }
        this.$message.success(this.title + '成功')
        // 再次渲染页面
        this.getPartners()
        // 关闭弹窗
        this.handleClose()
      })
    },

    // 合作商搜索
    async getPartners() {
      const res = await Partners(this.params)
      console.log(res)
      this.totalCount = Number(res.totalCount)
      this.totalPage = res.totalPage
      this.PartnerData = res.currentPageRecords
    },
    // 查询合作商
    onSubmit() {
      this.getPartners()
    },
    // 新建
    addbuild(val) {
      // this.ruleForm = val
      this.dialogVisible = true
    },
    // 关闭
    handleClose() {
      this.isShow = true
      this.dialogVisible = false
      // 关闭清空表单
      this.$refs.ruleForm.resetFields()
    },
    // 查看详情
    handleClick(val) {
      // console.log(val)
      this.ruleForm = val
      this.seeDetails = true
    },
    // 序号
    indexMethod(index) {
      return index + 1
    },
    // 翻页
    currentChangeFn(val) {
      this.params.pageIndex = val
      this.getPartners()
    }

  }

}
</script>

<style lang="scss" scoped>
.btn{
  background-color: #5f84ff;

}
// 头部固定
.search {
  display: flex;
  display: -webkit-box;
  background: #fff;
  height: 64px;
  -webkit-box-align: center;
  align-items: center;
  margin-bottom: 20px;
  padding-left: 17px;
  background-color: #fff;
}
.demo-form-inline {
  height: 36px;
}
// 新建
.result {
  padding: 20px 15px 19px 17px;
  background-color: #fff;
  .operation {
    margin-bottom: 20px;
    .one {
      background: linear-gradient(135deg,#ff9743,#ff5e20)!important;
      color: #fff;
    }
    .two{
          background-color: #fbf4f0!important;
    }
  }
}
.el-input{
  width: 90%;
}
</style>

